<?php

namespace App\Http\Controllers\System;

use App\Http\Requests\RoleRequest;
use App\Http\Controllers\Controller;
use App\Http\Resources\RoleCollection;
use App\Http\Resources\RoleResource;
use App\Models\Permission;
use App\Models\Role;
use Illuminate\Http\Request;

class RoleController extends Controller
{

    protected $model = null;

    public function __construct()
    {
        $this->model = new Role();
    }

    /**
     * Display a listing of the resource.
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(Request $request)
    {
        $perPage  = $request->get('limit', config('paginate.per_page'));
        $pageName = config('paginate.page_name');
        $page     = $request->get($pageName, 1);
        $rows     = $this->model::with('permissions')->orderByDesc('updated_at')->paginate($perPage, ['*'], $pageName, $page);
        return $this->success('get data success', RoleResource::collection($rows));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param RoleRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function store(RoleRequest $request)
    {
        $data = $request->all();
        $role = $this->model::create($data);
        if (!$role) {
            return $this->failure('failed to create role');
        }
        $permissions = $request->only('permissions');
        if (empty($permissions)) {
            $role->permissions()->detach();
        }
        $role->syncPermissions($permissions);
        return $this->success('create success', new RoleResource($role));
    }

    /**
     * Display the specified resource.
     *
     * @param  mixed  $type
     * @return \Illuminate\Http\JsonResponse
     */
    public function show($type)
    {
        switch (strtolower($type)) {
            case 'permissions':
                $result = Permission::all()->sortByDesc('sort');
                break;
            default:
                $result = [];
        }
        return $this->success('get data success', $result);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param RoleRequest $request
     * @param int $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function update(RoleRequest $request, $id)
    {
        $role = $this->model::find($id);
        if (empty($role)) {
            $this->failure("failed to get role#{$id}");
        }
        $data = $request->all();
        $res = $role->update($data);
        if ($res === false) {
            return $this->failure("failed to update role#{$id}");
        }
        $permissions = $request->only('permissions');
        if (empty($permissions)) {
            $role->permissions()->detach();
        }
        $role->syncPermissions($permissions);
        return $this->success('update success', new RoleResource($role));
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param int $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function destroy($id)
    {
        if (empty($id)) {
            return $this->failure('missing parameters');
        }
        $role = $this->model::find($id);
        if (!$role) {
            return $this->failure("failed to get role#{$id}");
        }
        $res = $role->delete();
        if (!$res) {
            return $this->failure("failed to delete role#{$id}");
        }
        return $this->success('delete success');
    }
}
